﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using YArchitech.LIB.YJKControls;
using HYCodeBase.WinForm;

namespace DBService
{
	public partial class DlgUserManage : System.Windows.Forms.Form
	{
		private DbUser CurrentUser { get; set; }

		public DlgUserManage(string group)
		{
			this.mGroup = group;
			this.InitializeComponent();
		}

		private void btnDelUser_Click(object sender, EventArgs e)
		{
			new DlgRemoveUser().ShowDialog(this);
		}

		private void btnSetRole_Click(object sender, EventArgs e)
		{
			new DlgSetRole(this.mGroup).ShowDialog(this);
		}

		private void btnAddRole_Click(object sender, EventArgs e)
		{
			new DlgAddRole(this.mGroup).ShowDialog(this);
		}

		private void btnDelRole_Click(object sender, EventArgs e)
		{
			new DlgRemoveRole(this.mGroup).ShowDialog(this);
		}

		private void btnSetRight_Click(object sender, EventArgs e)
		{
			new DlgSetRight(this.mGroup).ShowDialog(this);
		}

		private void DlgUserManage_Load(object sender, EventArgs e)
		{
			try
			{
				DbUser dbUser = null;
				string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
				string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "AutoUser", "value");
				try
				{
					dbUser = DbUser.GetUser(dataFromConfigFile, BimServerDB.Default);
				}
				catch
				{
				}
				if (dbUser == null)
				{
					DlgAdminLogin dlgAdminLogin = new DlgAdminLogin();
					dlgAdminLogin.txtName.Text = "admin";
					if (dlgAdminLogin.ShowDialog(this) == DialogResult.OK)
					{
						if (dlgAdminLogin.User != null)
						{
							if (dlgAdminLogin.User.IsAdmin())
							{
								this.CurrentUser = dlgAdminLogin.User;
								MessageBox.Show("登录成功！");
							}
							else
							{
								MessageBox.Show("请用系统管理员账号登陆。");
								XmlUtils.WriteConfigFile(fileName, "AutoUser", "value", "");
								XmlUtils.WriteConfigFile(fileName, "AutoPwd", "value", "");
								base.Close();
							}
						}
						else
						{
							base.Close();
						}
					}
					else
					{
						base.Close();
					}
				}
				else
				{
					this.CurrentUser = dbUser;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				base.Close();
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			new DlgChangePassword("admin").ShowDialog(this);
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (this.CurrentUser != null)
			{
				if (this.CurrentUser.NewMessageCount > 0)
				{
					this.tsmiCommunication.Text = "通讯(" + this.CurrentUser.NewMessageCount + ")";
					this.tsmiInbox.Text = "收件箱(" + this.CurrentUser.NewMessageCount + ")";
					return;
				}
				this.tsmiCommunication.Text = "通讯";
				this.tsmiInbox.Text = "收件箱";
			}
		}

		private void tsmiSent_Click(object sender, EventArgs e)
		{
			new DlgSendMessage(this.CurrentUser).ShowDialog(this);
		}

		private void tsmiInbox_Click(object sender, EventArgs e)
		{
			new DlgMessageBox(this.CurrentUser).ShowDialog(this);
		}

		private void 注销登录ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			XmlUtils.WriteConfigFile(fileName, "AutoUser", "value", "");
			XmlUtils.WriteConfigFile(fileName, "AutoPwd", "value", "");
			base.Close();
			base.Dispose();
			Process[] processesByName = Process.GetProcessesByName("Revit");
			if (processesByName.Length != 0)
			{
                WindowHandle owner = new WindowHandle(processesByName[0].MainWindowHandle);
				new DlgUserManage("FM").Show(owner);
			}
		}

		private void DlgUserManage_FormClosed(object sender, FormClosedEventArgs e)
		{
			base.Dispose();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private string mGroup = "";
	}
}
